<%--
    Document   : search
    Created on : Jul 1, 2011, 9:00:25 AM
    Author     : PhongLK60473
--%>

<%@page import="Object.BookProperties.Type"%>
<%@page import="Object.BookProperties.Author"%>
<%@page import="Object.Page"%>
<%@page import="Object.Order"%>
<%@page import="Object.Book"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%

            String strSearch = request.getParameter("txtSearch");
            String lastSearch;


            if(strSearch==null||strSearch.equals("")){
                strSearch = "%";
                lastSearch="";
            } else {
                lastSearch=strSearch;
                strSearch = "%"+strSearch+"%";
            }

            byte[] utf8 = strSearch.getBytes("UTF-32");
            String ustrSearch = new String(utf8,"UTF-32");

            String[] searchBy = request.getParameterValues("cbSearchBy");
            int intSearchBy;
            if(searchBy!=null&&searchBy.length>0){
                try {
                    intSearchBy = Integer.parseInt(searchBy[0]);
                } catch (NumberFormatException e){
                    intSearchBy=0;
                }
            } else {
                intSearchBy=0;
            }

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <style type="" >
            @import url("style/style.css");
            @import url("style/managebooks.style.css");
        </style>
    </head>
    <body>
        <%@include file="header.jsp" %>
        <%
                    if (!user.isAdmin()) {
                        response.sendRedirect("UserController");
                    }
        %>
        <div id="page">

        <% if(Page.isMessage(request)){ %>
            <div class="message"><%= Page.getMessage(request) %></div>
        <% } %>
        <div id="add-type">
                <form action="ManagerController" medthod="POST" id="add">
                    <div class="header">
                        <a href="">
                            <img src="images/icon_add_book.png"/>
                        Add New Book
                        </a>
                    </div>
                    <div class="content">
                        <table class="addbook">
                            <tr>
                                <td class="label">Title</td>
                                <td colspan="4">
                                    <input type="text" name="txtTitle" id="title" value=""/>
                                </td>
                            </tr>
                            <tr>
                                <td class="label">Description</td>
                                <td colspan="4">
                                    <textarea type="text" name="txtDescription" ></textarea>
                                </td>
                            </tr>
                            <tr>
                                <td class="label">&nbsp;</td>
                                <td class="price">
                                    <span class="label">Price</span>
                                    <input type="text" name="txtPrice" id="price" value=""/>
                                </td>
                                <td class="author">
                                    <span class="label">Author</span>
                                    <select name="cbAuthor">
                                    <%
                                        Author[] authors = Author.AllAuthors();
                                        for(int i=0;i<authors.length;i++){
                                            %>
                                            <option value="<%= authors[i].getAuthorId() %>">
                                                <%= authors[i].getAuthorName() %></option>
                                            <%
                                        }
                                    %>
                                    </select>
                                </td>
                                <td class="type">
                                    <span class="label">Type</span>
                                    <select name="cbType">
                                    <%
                                        Type[] types = Type.AllTypes();
                                        for(int i=0;i<types.length;i++){
                                            %>
                                            <option value="<%= types[i].getTypeId() %>">
                                                <%= types[i].getTypeName() %></option>
                                            <%
                                        }
                                    %>
                                    </select>
                                </td>
                                <td><input type="submit" name="action" value="Add Book"/> </td>
                            </tr>
                        </table>
                    </div>
                </form>
            </div>
        <div id="search-pnl">
            <div class="float-left">
                <div class="label" id="label">
                    Search :
                </div>
            </div>
            <div class="float-right">
                <form action="ManagerController" method="POST">
                    <input type="text" name="txtSearch" id="s" value="<%=lastSearch%>">
                    <select name="cbSearchBy">
                        <option value="0" <%=(intSearchBy==0?"Selected":"")%>>Title</option>
                        <option value="1" <%=(intSearchBy==1?"Selected":"")%>>Author</option>
                        <option value="2" <%=(intSearchBy==2?"Selected":"")%>>Type</option>
                    </select>
                    <input type="submit" name="action" value="Search"/>
                </form>
                    <br>
                (leave blank mean view all)
            </div>

        </div>
        <br>
        <%

            Book[] result = Book.searchBook(ustrSearch, intSearchBy);

        %>

        <div id="search-result">
            <table border="1" class="books">
                <thead>
                    <tr class="title">
                        <th class=" Id">Id.</td>
                        <th class=" Title">Title</td>
                        <th class=" Author">Author</td>
                        <th class=" Type">Type</td>
                        <th class=" Action">Action</td>
                    </tr>
                </thead>
                <tbody>
                    <%
                    for(int i=0;i<result.length;i++){
                        Book book=(Book) result[i];
                    %>
                    <tr class="data">
                        <td class="Id"><%= book.getBookId() %></td>
                        <td class="Title">
                            <a href="ManagerController?action=
EditBook&bookid=<%= book.getBookId() %>"
                               title="Click to Edit
Price : <%= book.getPrice() %>$
<%= book.getDescription() %>">
                            <%= book.getBookTitle() %>
                            </a>
                        </td>
                        <td class="Author"><%= book.getBookAuthor() %></td>
                        <td class="Type"><%= book.getBookType() %></td>
                        <td class="Action">
                            <a href="ManagerController?action=EditBook&bookid=<%= book.getBookId() %>"><img src="images/icon_edit_book.png"/></a>
                            <a href="ManagerController?action=DeleteBook&bookid=<%= book.getBookId() %>"><img src="images/icon_delete_book.jpg"/></a>
                        </td>
                    </tr>
                    <%
                    }
                    %>
                </tbody>
            </table>
        </div>
        </div>
        <script>
            document.getElementById("add").onsubmit=function(){
                try {
                    if(document.getElementById("title").value.trim()==""){
                        document.getElementById("title").focus();
                        return false;
                    } else {
                        price = document.getElementById("price").value.trim();
                        if(price==""){
                            document.getElementById("price").focus();
                            return false;
                        }
                        
                        if(price/1!=price){
                            document.getElementById("price").value="";
                            document.getElementById("price").focus();
                            return false;
                        }
                    }
                }catch(e){
                    console.log(e);
                    return false;
                }
            return true;
            }

            document.onsubmit=function(e){
                
            }
        </script>
    </body>
</html>
